package com.example.bailin.giftsay.sort;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.baseclass.BaseFragment;
import com.example.bailin.giftsay.entity.RaidersDownEntity;
import com.example.bailin.giftsay.entity.SortColumnEntity;
import com.example.bailin.giftsay.sort.activity.GLSkipActivity;
import com.example.bailin.giftsay.sort.activity.LmSkipActivity;
import com.example.bailin.giftsay.sort.activity.PLorDXActivity;
import com.example.bailin.giftsay.sort.activity.SoryColumnActivity;
import com.example.bailin.giftsay.sort.adapter.LanMuAdapter;
import com.example.bailin.giftsay.sort.adapter.PinLeiAdapter;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;
import com.example.bailin.giftsay.tool.UrlProvide;

/**
 * Created by bailin on 16/8/17.
 * <p/>
 * 攻略
 */
public class StrategySortFragment extends BaseFragment implements View.OnClickListener {


    private RecyclerView rvLanmu;
    private RecyclerView rvPinlei;
    private RecyclerView rvFengge;
    private RecyclerView rvDuixiang;
    private Intent intent;

    @Override
    protected int setLayout() {
        return R.layout.fragment_strategysort;
    }

    @Override
    protected void initView() {
        rvLanmu = bindView(R.id.rv_lanmu_id);
        rvPinlei = bindView(R.id.rv_pinlei_id);
        rvFengge = bindView(R.id.rv_fengge_id);
        rvDuixiang = bindView(R.id.rv_duixiang_id);
        TextView tvColumnViewall = bindView(R.id.tv_column_viewall);// 栏目的查看全部
        TextView tvPinleiViewall = bindView(R.id.tv_pinlei_viewall);// 品类的查看全部
        TextView tvDuiXiangViewall = bindView(R.id.tv_duixiang_viewall);// 对象的查看全部
        tvColumnViewall.setOnClickListener(this);
        tvPinleiViewall.setOnClickListener(this);
        tvDuiXiangViewall.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 跳转到栏目的查看全部
            case R.id.tv_column_viewall:
                Intent intent = new Intent(getContext(), SoryColumnActivity.class);
                startActivity(intent);
                break;
            // 跳转到品类的全部 传递个1
            case R.id.tv_pinlei_viewall:
                Intent intent1 = new Intent(getContext(), PLorDXActivity.class);
                intent1.putExtra("index", 1);
                startActivity(intent1);
                break;
            // 跳转到对象的全部 传递个2
            case R.id.tv_duixiang_viewall:
                Intent intent2 = new Intent(getContext(), PLorDXActivity.class);
                intent2.putExtra("index", 2);
                startActivity(intent2);
                break;
        }
    }

    /**
     * 栏目
     */
    @Override
    protected void initData() {
        tool().getData(UrlProvide.SORT_STRATEGY_COLUMN, SortColumnEntity.class, new NetTool.NetInterface<SortColumnEntity>() {
            @Override
            public void onSuccess(SortColumnEntity soryColumnEntity) {
                GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 3);
                manager.setOrientation(LinearLayoutManager.HORIZONTAL);
                LanMuAdapter lanMuAdapter = new LanMuAdapter();
                lanMuAdapter.setEntity(soryColumnEntity);
                lanMuAdapter.setClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {
                        Intent intent = new Intent(MyApp.getContext(), LmSkipActivity.class);
                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        startActivity(intent);
                    }
                });
                rvLanmu.setLayoutManager(manager);
                rvLanmu.setAdapter(lanMuAdapter);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
        intent = new Intent(MyApp.getContext(), GLSkipActivity.class);
        tool().getData(UrlProvide.RAIDERS_DOWN, RaidersDownEntity.class, new NetTool.NetInterface<RaidersDownEntity>() {
            @Override
            public void onSuccess(RaidersDownEntity raidersDownEntity) {

                // adapter参数如果是0 则页面加载6个
                GridLayoutManager manager = new GridLayoutManager(MyApp.getContext(), 2);
                PinLeiAdapter adapter0 = new PinLeiAdapter(0);
                adapter0.setEntity(raidersDownEntity);
                adapter0.setI(0);
                adapter0.setLmClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {

                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        startActivity(intent);
                    }
                });
                rvPinlei.setLayoutManager(manager);
                rvPinlei.setAdapter(adapter0);

                GridLayoutManager manager1 = new GridLayoutManager(MyApp.getContext(), 2);
                PinLeiAdapter adapter1 = new PinLeiAdapter(0);
                adapter1.setEntity(raidersDownEntity);
                adapter1.setI(1);
                adapter1.setLmClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {
                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        startActivity(intent);
                    }
                });
                rvFengge.setLayoutManager(manager1);
                rvFengge.setAdapter(adapter1);

                GridLayoutManager manager2 = new GridLayoutManager(MyApp.getContext(), 2);
                PinLeiAdapter adapter2 = new PinLeiAdapter(0);
                adapter2.setEntity(raidersDownEntity);
                adapter2.setI(2);
                adapter2.setLmClickListener(new LmClickListener() {
                    @Override
                    public void onClick(int id, String title) {
                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        startActivity(intent);
                    }
                });
                rvDuixiang.setLayoutManager(manager2);
                rvDuixiang.setAdapter(adapter2);
            }

            @Override
            public void onError(String errorMsg) {
            }
        });
    }

}
